package com.fenghuajueli.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.fenghuajueli.libbasecoreui.recycler.show
import com.fenghuajueli.module_home.R
import com.fenghuajueli.module_home.databinding.ItemHomeVideoListBinding
import com.fenghuajueli.module_home.room.homevideo.HomeVideo
import com.fenghuajueli.module_home.utils.startPlayVideo


/**
 * @description
 * @date 2022/10/31
 */
class HomeVideoListRvAdapter(val list: List<HomeVideo>, val title:String) :
    RecyclerView.Adapter<HomeVideoListRvAdapter.mViewHolder>() {

    var imgList1 = arrayListOf(
        R.mipmap.zrpd_img_1,
        R.mipmap.zrpd_img_2,
        R.mipmap.zrpd_img_3,
        R.mipmap.zrpd_img_4,
        R.mipmap.zrpd_img_5,
        R.mipmap.zrpd_img_6,
        R.mipmap.zrpd_img_7,
        R.mipmap.zrpd_img_8,
        R.mipmap.zrpd_img_9,
    )
    var imgList2 = arrayListOf(
        R.mipmap.yweg_img_1,
        R.mipmap.yweg_img_2,
        R.mipmap.yweg_img_3,
        R.mipmap.yweg_img_4,
        R.mipmap.yweg_img_5,
        R.mipmap.yweg_img_6,
        R.mipmap.yweg_img_7,
        R.mipmap.yweg_img_8,
        R.mipmap.yweg_img_9,
    )
    var imgList3 = arrayListOf(
        R.mipmap.ywhb_img_1,
        R.mipmap.ywhb_img_2,
        R.mipmap.ywhb_img_3,
        R.mipmap.ywhb_img_4,
        R.mipmap.ywhb_img_5,
        R.mipmap.ywhb_img_6,
        R.mipmap.ywhb_img_7,
        R.mipmap.ywhb_img_8,
        R.mipmap.ywhb_img_9,
    )

     open class mViewHolder(val binding: ItemHomeVideoListBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): mViewHolder {
        return mViewHolder(ItemHomeVideoListBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun onBindViewHolder(holder: mViewHolder, position: Int) {
        val imgList = when(title){
            "英文绘本" -> imgList3
            "英文儿歌" -> imgList2
            else -> imgList1
        }

        val currentPosition = holder.layoutPosition*3
        holder.binding.apply {
            myTextView1.text = list[currentPosition].name
            ivImg1.setImageResource(imgList[currentPosition%imgList.size])
            ivImg1.setOnClickListener {
                startPlayVideo(list[currentPosition].vid,1,list[currentPosition].itemVid)
            }
            if (currentPosition+1>list.size-1){
                group2.visibility = ViewGroup.INVISIBLE
            } else {
                group2.show()
                myTextView2.text = list[currentPosition+1].name
                ivImg2.setImageResource(imgList[(currentPosition+1)%imgList.size])
                ivImg2.setOnClickListener {
                    startPlayVideo(list[currentPosition+1].vid,1,list[currentPosition+1].itemVid)
                }
            }

            if (currentPosition+2>list.size-1){
                group3.visibility = ViewGroup.INVISIBLE
            } else {
                group3.show()
                myTextView3.text = list[currentPosition+2].name
                ivImg3.setImageResource(imgList[(currentPosition+2)%imgList.size])
                ivImg3.setOnClickListener {
                    startPlayVideo(list[currentPosition+2].vid,1,list[currentPosition+2].itemVid)
                }
            }

        }

    }

    override fun getItemCount(): Int {
        return (list.size + 2) / 3
    }
}